package com.atos.optical.common;

import cn.hutool.core.util.StrUtil;
import com.atos.optical.Enums.StaticEnums;
import com.atos.optical.model.po.SysUserInfo;
import com.atos.optical.utils.CacheUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;

import javax.servlet.http.HttpServletRequest;

/**
 * @author wanghra
 * @version 1.0
 * @description
 * @date 2025/8/11
 */
@Slf4j
public class BaseHttpController {
    @Autowired
    protected HttpServletRequest request;
    @Autowired
    protected MessageSource messageSource;
    
    public static String getToken(HttpServletRequest request) {
        String token = request.getHeader(StaticEnums.TOKEN_HEADER);
        if (StrUtil.isBlank(token)) {
            log.info("class:[BaseHttpController],method:[getToken],message:[获取token为空]");
            return null;
        }
        return token;
    }
    
    public static String getUserName(HttpServletRequest request) {
        String token = getToken(request);
        SysUserInfo userInfo = CacheUtil.get(token, SysUserInfo.class);
        return userInfo.getUserName();
    }
}
